package com.saint.tvtest.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.saint.tvtest.R;
import com.saint.tvtest.Utils.HttpUtil;
import com.saint.tvtest.adapter.CatPagerAdapter;
import com.saint.tvtest.bean.CatContent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/2/27.
 */

public class Main3Activity extends Activity {
    private Activity act;
    private Toast toast;
    private ViewPager viewPager;
    private CatContent catContent;
    private List<CatContent.DataBean> dataBeans;
    private Callback<String> subCatContentCall;
    private TextView tvTitle, tvPagerNum, tvBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_view_page);
        initView();
        initCallBack();
        getSubCatContent();
    }

    private void initView() {
        act = this;
        viewPager = findViewById(R.id.view_pager_content);
        tvTitle = findViewById(R.id.tv_title);
        tvPagerNum = findViewById(R.id.tv_pager_num);
        tvBack = findViewById(R.id.tv_back);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    private void initCallBack() {
        subCatContentCall = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && null != response.body()) {
                    Log.e("HTTP", "请求结果： " + response.body());
                    catContent = new Gson().fromJson(response.body(), CatContent.class);
                    dataBeans = catContent.getData();
                    setAdapterToViewPager(catContent.getData());
                    tvPagerNum.setText(1 + " / " + dataBeans.size());
                } else {
                    onFailure(call, new Throwable("response body is null"));
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                tShort("加载失败：" + t.getMessage());
                Log.e("LOADINGData", t.getMessage());
            }
        };
    }


    public void tShort(String msg) {
        if (null == toast) {
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setText(msg);
        }

        toast.show();

    }


    private void getSubCatContent() {
        String title = getIntent().getStringExtra("title");
        String catId = getIntent().getStringExtra("cat_id");

        tvTitle.setText(title);

        Map<String, String> map = new ArrayMap<>();
        map.put("cat_id", catId);

        Call<String> catContent = HttpUtil.getInstance(Main2Activity.url).getSubCatContent(map);
        catContent.enqueue(subCatContentCall);
    }


    private void setAdapterToViewPager(final List<CatContent.DataBean> dataBeans) {

        viewPager.setAdapter(new CatPagerAdapter(act, dataBeans));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvPagerNum.setText(position + 1 + " / " + dataBeans.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
